/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.sample;

import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Picker;
import ohos.agp.components.RadioButton;
import ohos.agp.components.RadioContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.global.icu.text.SimpleDateFormat;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateLongClickListener;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.prolificinteractive.materialcalendarview.sample.tools.ToastsUtils;
import com.prolificinteractive.materialcalendarview.sample.tools.WeekDay;

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import org.threeten.bp.format.DateTimeFormatter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @since 2021-07-12
 */
public class DynamicSettersAbility extends BaseAbility implements Component.ClickedListener, OnDateLongClickListener, OnDateSelectedListener {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("MMM dd日 EEE");
    private static final DateTimeFormatter FORMATTERYEAR = DateTimeFormatter.ofPattern("yyyy年");
    private SimpleDateFormat sdfyear = new SimpleDateFormat("yyyy年");
    private SimpleDateFormat sdfmonthday = new SimpleDateFormat("MM月dd日,");

    private Button button_other_dates;
    private Button button_months;
    private Button button_weeks;
    private Button button_selection_mode;
    private Button button_toggle_topbar;
    private Button button_change_orientation;
    private Button button_previous;
    private Button button_next;
    private Button button_min_date;
    private Button button_max_date;
    private Button button_selected_date;
    private Button button_set_first_day;
    private Button button_set_tile_size;
    private Button button_set_colors;
    private Button button_set_width_height;
    private Button button_clear_selection;

    private Checkbox check_text_appearance;
    private Checkbox check_page_enabled;
    private Checkbox animate_mode_transition;
    private Checkbox show_week_days;
    private Checkbox enable_save_current_position;
    private Checkbox dynamic_height_enabled;

    private CommonDialog otherDialog;
    private CommonDialog selectionmodeDialog;
    private CommonDialog minDialog;
    private CommonDialog maxDialog;
    private CommonDialog selectiondateDialog;
    private CommonDialog tileDialog;
    private CommonDialog widthandheightDialog;

    private MaterialCalendarView widget;
    private LocalDate calendarmin = LocalDate.now();
    private LocalDate calendarmax = LocalDate.now();
    private LocalDate calendarselcter;

    private Date date;
    private Random random = new Random();
    private int currentTile;

    private Checkbox action_empty;
    private Checkbox action_loading;
    private Checkbox action_error;
    private Checkbox action_content;
    boolean cktag;
    private CalendarDay datesle;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        toolbarTitle.setText("Dynamic Setters Test");
        widget = (MaterialCalendarView) findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);
        button_other_dates = (Button) findComponentById(ResourceTable.Id_button_other_dates);
        button_other_dates.setClickedListener(this);
        button_months = (Button) findComponentById(ResourceTable.Id_button_months);
        button_months.setClickedListener(this);
        button_weeks = (Button) findComponentById(ResourceTable.Id_button_weeks);
        button_weeks.setClickedListener(this);
        button_selection_mode = (Button) findComponentById(ResourceTable.Id_button_selection_mode);
        button_selection_mode.setClickedListener(this);
        button_toggle_topbar = (Button) findComponentById(ResourceTable.Id_button_toggle_topbar);
        button_toggle_topbar.setClickedListener(this);
        button_change_orientation = (Button) findComponentById(ResourceTable.Id_button_change_orientation);
        button_change_orientation.setClickedListener(this);
        button_previous = (Button) findComponentById(ResourceTable.Id_button_previous);
        button_previous.setClickedListener(this);
        button_next = (Button) findComponentById(ResourceTable.Id_button_next);
        button_next.setClickedListener(this);
        button_min_date = (Button) findComponentById(ResourceTable.Id_button_min_date);
        button_min_date.setClickedListener(this);
        button_max_date = (Button) findComponentById(ResourceTable.Id_button_max_date);
        button_max_date.setClickedListener(this);
        button_selected_date = (Button) findComponentById(ResourceTable.Id_button_selected_date);
        button_selected_date.setClickedListener(this);
        button_set_first_day = (Button) findComponentById(ResourceTable.Id_button_set_first_day);
        button_set_first_day.setClickedListener(this);
        button_set_tile_size = (Button) findComponentById(ResourceTable.Id_button_set_tile_size);
        button_set_tile_size.setClickedListener(this);
        button_set_colors = (Button) findComponentById(ResourceTable.Id_button_set_colors);
        button_set_colors.setClickedListener(this);
        button_set_width_height = (Button) findComponentById(ResourceTable.Id_button_set_width_height);
        button_set_width_height.setClickedListener(this);
        button_clear_selection = (Button) findComponentById(ResourceTable.Id_button_clear_selection);
        button_clear_selection.setClickedListener(this);

        check_page_enabled = (Checkbox) findComponentById(ResourceTable.Id_check_page_enabled);
        check_page_enabled.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.setPagingEnabled(b);
            }
        });
        check_page_enabled.setChecked(true);

        animate_mode_transition = (Checkbox) findComponentById(ResourceTable.Id_animate_mode_transition);
        animate_mode_transition.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.setPagingEnabled(b);
            }
        });
        animate_mode_transition.setChecked(true);

        show_week_days = (Checkbox) findComponentById(ResourceTable.Id_show_week_days);
        show_week_days.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.state().edit().setShowWeekDays(b).commit();
            }
        });
        show_week_days.setChecked(true);

        enable_save_current_position = (Checkbox) findComponentById(ResourceTable.Id_enable_save_current_position);
        enable_save_current_position.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.state().edit().isCacheCalendarPositionEnabled(b).commit();
            }
        });
        dynamic_height_enabled = (Checkbox) findComponentById(ResourceTable.Id_dynamic_height_enabled);
        dynamic_height_enabled.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.setDynamicHeightEnabled(b);
            }
        });

//        最顶部的checkbox
        check_text_appearance = (Checkbox) findComponentById(ResourceTable.Id_check_text_appearance);
        check_text_appearance.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                if (b) {
                    action_empty.setChecked(true);
                    action_loading.setChecked(true);
                    action_error.setChecked(true);
                    widget.setShowOtherDates(7);
                    widget.setHeaderTextAppearanceSize(18);
                    widget.setDateTextAppearanceSize(18);
                    widget.setWeekDayTextAppearanceSize(18);
                } else {
                    action_empty.setChecked(false);
                    action_loading.setChecked(false);
                    action_error.setChecked(false);
                    widget.setShowOtherDates(0);
                    widget.setHeaderTextAppearanceSize(14);
                    widget.setDateTextAppearanceSize(14);
                    widget.setWeekDayTextAppearanceSize(14);
                }
                cktag = b;
            }
        });
        date = new Date();
        showotherdialog();
        showselectionmodeDialog();
        showminDialog();
        showmaxDialog();
        showselectiondateialog();
        showtileDialog();
        showwhiteandheightDialog();
        widget.setOnTitleClickListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component var1) {
                new ToastsUtils.Builder(getContext())
                    .setToastText("Today is the day")
                    .build().show();
            }
        });
        widget.setOnDateChangedListener(this);
        widget.setOnDateLongClickListener(this);

    }

    @Override
    int contentLayoutRes() {
        return ResourceTable.Layout_ability_dynamic_setters;
    }

    @Override
    public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
        this.datesle = date;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button_other_dates:
                otherDialog.show();
                break;
            case ResourceTable.Id_button_months:
                widget.state().edit().setCalendarDisplayMode(CalendarMode.MONTHS).commit();
                break;
            case ResourceTable.Id_button_weeks:
                widget.state().edit().setCalendarDisplayMode(CalendarMode.WEEKS).commit();
                break;
            case ResourceTable.Id_button_selection_mode:
                selectionmodeDialog.show();
                break;
            case ResourceTable.Id_button_toggle_topbar:
                widget.setTopbarVisible(!widget.getTopbarVisible());
                break;
            case ResourceTable.Id_button_change_orientation:
                widget.setTitleAnimationOrientation(
                    widget.getTitleAnimationOrientation() == MaterialCalendarView.VERTICAL
                        ? MaterialCalendarView.HORIZONTAL
                        : MaterialCalendarView.VERTICAL);
                new ToastsUtils.Builder(getContext())
                    .setToastText(widget.getTitleAnimationOrientation() == MaterialCalendarView.VERTICAL
                        ? "Vertical"
                        : "Horizontal")
                    .build().show();
                break;
            case ResourceTable.Id_button_previous:
                widget.goToPrevious();
                break;
            case ResourceTable.Id_button_next:
                widget.goToNext();
                break;
            case ResourceTable.Id_button_min_date:
                minDialog.show();
                break;
            case ResourceTable.Id_button_max_date:
                maxDialog.show();
                break;
            case ResourceTable.Id_button_selected_date:
                selectiondateDialog.show();
                break;
            case ResourceTable.Id_button_set_first_day:
                int index = random.nextInt(DAYS_OF_WEEK.length);
                widget.state().edit().setFirstDayOfWeek(DAYS_OF_WEEK[index]).commit();
                break;
            case ResourceTable.Id_button_set_tile_size:

                tileDialog.show();
                break;
            case ResourceTable.Id_button_set_colors:
                int indexcolor = random.nextInt(COLORS.length);
                widget.setSelectionColor(COLORS[indexcolor].getValue());
                break;
            case ResourceTable.Id_button_set_width_height:
                widthandheightDialog.show();
                break;
            case ResourceTable.Id_button_clear_selection:
                widget.clearSelection();
                break;
            default:
                throw new IllegalStateException("Unexpected value: ");
        }
    }


    private void showotherdialog() {

//        final MaterialCalendarView.ShowOtherDates[] itemValues = {
//            MaterialCalendarView.ShowOtherDates.SHOW_OTHER_MONTHS,
//            MaterialCalendarView.ShowOtherDates.SHOW_OUT_OF_RANGE,
//            MaterialCalendarView.ShowOtherDates.SHOW_DECORATED_DISABLED,
//            MaterialCalendarView.ShowOtherDates.SHOW_NONE
//        };
        int showOtherDates = widget.getShowOtherDates();

        boolean[] initSelections = {
            MaterialCalendarView.showOtherMonths(showOtherDates),
            MaterialCalendarView.showOutOfRange(showOtherDates),
            MaterialCalendarView.showDecoratedDisabled(showOtherDates),
            widget.allowClickDaysOutsideCurrentMonth()
        };
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_other, null, false);
        otherDialog = initdialog(mComponent, this);

        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                otherDialog.hide();
            }
        });

        action_empty = (Checkbox) mComponent.findComponentById(ResourceTable.Id_action_empty);
        action_empty.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                int showOtherDates = widget.getShowOtherDates();
                if (b) {
                    showOtherDates = showOtherDates + 1;
                } else {
                    showOtherDates = showOtherDates - 1;
                }
                widget.setShowOtherDates(showOtherDates);
            }
        });
        action_loading = (Checkbox) mComponent.findComponentById(ResourceTable.Id_action_loading);
        action_loading.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                int showOtherDates = widget.getShowOtherDates();
                if (b) {
                    showOtherDates = showOtherDates + 2;
                } else {
                    showOtherDates = showOtherDates - 2;
                }
                widget.setShowOtherDates(showOtherDates);
            }
        });
        action_error = (Checkbox) mComponent.findComponentById(ResourceTable.Id_action_error);
        action_error.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                int showOtherDates = widget.getShowOtherDates();
                if (b) {
                    showOtherDates = showOtherDates + 4;
                } else {
                    showOtherDates = showOtherDates - 4;
                }
                widget.setShowOtherDates(showOtherDates);
            }
        });
        action_error.setChecked(true);
        action_content = (Checkbox) mComponent.findComponentById(ResourceTable.Id_action_content);
        action_content.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean b) {
                widget.setAllowClickDaysOutsideCurrentMonth(b);
            }
        });
        action_content.setChecked(true);
    }

    private void showselectionmodeDialog() {
        RadioContainer mRadioContainer;
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_selectionmode, null, false);
        selectionmodeDialog = initdialog(mComponent, this);
        StateElement mstateNone = new StateElement();
        mstateNone.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateNone.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateSingle = new StateElement();
        mstateSingle.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateSingle.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateMultiple = new StateElement();
        mstateMultiple.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateMultiple.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        StateElement mstateRange = new StateElement();
        mstateRange.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_checked_black_24dp)));
        mstateRange.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, new PixelMapElement(getPixelMap(ResourceTable.Media_baseline_radio_button_unchecked_black_24dp)));
        RadioButton radioNone = (RadioButton) mComponent.findComponentById(ResourceTable.Id_action_empty);
        RadioButton radioSingle = (RadioButton) mComponent.findComponentById(ResourceTable.Id_action_loading);
        RadioButton radioMultiple = (RadioButton) mComponent.findComponentById(ResourceTable.Id_action_error);
        RadioButton radioRange = (RadioButton) mComponent.findComponentById(ResourceTable.Id_action_content);
        radioNone.setButtonElement(mstateNone);
        radioSingle.setButtonElement(mstateSingle);
        radioMultiple.setButtonElement(mstateMultiple);
        radioRange.setButtonElement(mstateRange);
        radioSingle.setChecked(true);
        mRadioContainer = (RadioContainer) mComponent.findComponentById(ResourceTable.Id_rc_group);
        mRadioContainer.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int i) {
                if (i == 1) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_NONE);
                    selectionmodeDialog.hide();
                } else if (i == 2) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_SINGLE);
                    selectionmodeDialog.hide();
                } else if (i == 3) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_MULTIPLE);
                    selectionmodeDialog.hide();
                } else if (i == 4) {
                    widget.setSelectionMode(MaterialCalendarView.SelectionMode.SELECTION_MODE_RANGE);
                    selectionmodeDialog.hide();
                } else {
                    throw new IllegalStateException("Unexpected value: ");
                }
            }
        });
        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                selectionmodeDialog.hide();
            }
        });

    }

    private void showminDialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_three_calendrview, null, false);
        minDialog = initdialog(mComponent, this);

        MaterialCalendarView widget;
        widget = (MaterialCalendarView) mComponent.findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);

        Text tv_sure;
        Text tv_canel;
        Text text_year;
        Text text_monthday;
        tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DynamicSettersAbility.this.widget.state().edit()
                    .setMinimumDate(calendarmin)
                    .commit();
                minDialog.hide();
            }
        });
        tv_canel = (Text) mComponent.findComponentById(ResourceTable.Id_tv_canel);
        tv_canel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                minDialog.hide();
            }
        });
        text_year = (Text) mComponent.findComponentById(ResourceTable.Id_text_year);
        text_year.setText(sdfyear.format(date));
        text_monthday = (Text) mComponent.findComponentById(ResourceTable.Id_text_monthday);
        text_monthday.setText(sdfmonthday.format(date) + WeekDay.getWeekDayString());
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
                calendarmin = date.getDate();
                text_year.setText(FORMATTERYEAR.format(date.getDate()));
                text_monthday.setText(FORMATTER.format(date.getDate()));
            }
        });
        final LocalDate calendar = LocalDate.now();
        widget.setSelectedDate(calendar);
        DirectionalLayout mDirectionalLayout = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDirectionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                minDialog.hide();
            }
        });

    }

    private void showmaxDialog() {

        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_three_calendrview, null, false);
        maxDialog = initdialog(mComponent, this);

        MaterialCalendarView widget;
        widget = (MaterialCalendarView) mComponent.findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);

        Text tv_sure;
        Text tv_canel;
        Text text_year;
        Text text_monthday;
        tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DynamicSettersAbility.this.widget.state().edit()
                    .setMaximumDate(calendarmax)
                    .commit();
                maxDialog.hide();
            }
        });
        tv_canel = (Text) mComponent.findComponentById(ResourceTable.Id_tv_canel);
        tv_canel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                maxDialog.hide();
            }
        });
        text_year = (Text) mComponent.findComponentById(ResourceTable.Id_text_year);
        text_year.setText(sdfyear.format(date));
        text_monthday = (Text) mComponent.findComponentById(ResourceTable.Id_text_monthday);
        text_monthday.setText(sdfmonthday.format(date) + WeekDay.getWeekDayString());
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
                calendarmax = date.getDate();
                text_year.setText(FORMATTERYEAR.format(date.getDate()));
                text_monthday.setText(FORMATTER.format(date.getDate()));
            }
        });
        final LocalDate calendar = LocalDate.now();
        widget.setSelectedDate(calendar);


        DirectionalLayout mDirectionalLayout = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDirectionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                maxDialog.hide();
            }
        });
    }

    private void showselectiondateialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_three_calendrview, null, false);
        selectiondateDialog = initdialog(mComponent, this);

        MaterialCalendarView widget;
        widget = (MaterialCalendarView) mComponent.findComponentById(ResourceTable.Id_calendarView);
        widget.setSelectionColor(COLOR_ACCENT);

        Text tv_sure;
        Text tv_canel;
        Text text_year;
        Text text_monthday;
        tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                DynamicSettersAbility.this.widget.setSelectedDate(calendarselcter);
                selectiondateDialog.hide();
            }
        });
        tv_canel = (Text) mComponent.findComponentById(ResourceTable.Id_tv_canel);
        tv_canel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                selectiondateDialog.hide();
            }
        });
        text_year = (Text) mComponent.findComponentById(ResourceTable.Id_text_year);
        text_year.setText(sdfyear.format(date));
        text_monthday = (Text) mComponent.findComponentById(ResourceTable.Id_text_monthday);
        text_monthday.setText(sdfmonthday.format(date) + WeekDay.getWeekDayString());
        widget.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(MaterialCalendarView widget, CalendarDay date, boolean selected) {
                calendarselcter = date.getDate();
                text_year.setText(FORMATTERYEAR.format(date.getDate()));
                text_monthday.setText(FORMATTER.format(date.getDate()));
            }
        });
        final LocalDate calendar = LocalDate.now();
        widget.setSelectedDate(calendar);
        DirectionalLayout mDirectionalLayout = (DirectionalLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDirectionalLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                selectiondateDialog.hide();
            }
        });
    }

    private void showtileDialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_picker, null, false);
        tileDialog = initdialog(mComponent, this);

        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        Picker mPicker = (Picker) mComponent.findComponentById(ResourceTable.Id_picker);
        List<String> list = setData();
        mPicker.setMinValue(0);
        mPicker.setMaxValue(list.size() - 1);
        Picker.Formatter provinceFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return list.get(i);
            }
        };
        mPicker.setFormatter(provinceFormatter);
        mPicker.setValue(20);
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                tileDialog.hide();
                currentTile = Integer.parseInt(list.get(mPicker.getValue()));
                widget.setTileSizeDp(currentTile);
                if (datesle != null) {
                    widget.setSelectedDate(datesle);
                } else {
                    datesle = CalendarDay.today();
                    widget.setSelectedDate(datesle);
                    widget.clearSelection();
                    datesle = null;
                }
            }
        });
        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                tileDialog.hide();
            }
        });
    }

    private void showwhiteandheightDialog() {
        Component mComponent = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_commondialog_set_twopicker, null, false);
        widthandheightDialog = initdialog(mComponent, this);

        Picker mPickerw = (Picker) mComponent.findComponentById(ResourceTable.Id_pickerw);
        List<String> listw = setData();
        mPickerw.setMinValue(0);
        mPickerw.setMaxValue(listw.size() - 1);
        Picker.Formatter provinceFormatterw = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return listw.get(i);
            }
        };
        mPickerw.setFormatter(provinceFormatterw);
        mPickerw.setValue(20);

        Picker mPickerh = (Picker) mComponent.findComponentById(ResourceTable.Id_pickerh);
        List<String> listh = setData();
        mPickerh.setMinValue(0);
        mPickerh.setMaxValue(listh.size() - 1);
        Picker.Formatter provinceFormatterh = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return listh.get(i);
            }
        };
        mPickerh.setFormatter(provinceFormatterh);
        mPickerh.setValue(20);

        DependentLayout mDependentLayout = (DependentLayout) mComponent.findComponentById(ResourceTable.Id_mDirLayout);
        mDependentLayout.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                widthandheightDialog.hide();
            }
        });
        Text tv_sure = (Text) mComponent.findComponentById(ResourceTable.Id_tv_sure);
        tv_sure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                widthandheightDialog.hide();
                widget.setTileHeightDp(Integer.parseInt(listh.get(mPickerh.getValue())));
                widget.setTileWidthDp(Integer.parseInt(listw.get(mPickerw.getValue())));
                if (datesle != null) {
                    widget.setSelectedDate(datesle);
                } else {
                    datesle = CalendarDay.today();
                    widget.setSelectedDate(datesle);
                    widget.clearSelection();
                    datesle = null;
                }
            }
        });
    }

    private List<String> setData() {
        List<String> list = new ArrayList<String>();
        for (int i = 24; i < 65; i++) {
            list.add(i + "");
        }
        return list;
    }

    @Override
    public void onDateLongClick(MaterialCalendarView widget, CalendarDay date) {
        new ToastsUtils.Builder(getContext())
            .setToastText(FORMATTER.format(date.getDate()))
            .build().show();
    }

    private static final DayOfWeek[] DAYS_OF_WEEK = {
        DayOfWeek.SUNDAY,
        DayOfWeek.MONDAY,
        DayOfWeek.TUESDAY,
        DayOfWeek.WEDNESDAY,
        DayOfWeek.THURSDAY,
        DayOfWeek.FRIDAY,
        DayOfWeek.SATURDAY,
    };
    private static final Color[] COLORS = {
        Color.BLACK,
        Color.BLUE,
        Color.CYAN,
        Color.DKGRAY,
        Color.GRAY,
        Color.GREEN,
        Color.LTGRAY,
        Color.MAGENTA,
        Color.RED,
        Color.TRANSPARENT,
        Color.WHITE,
        Color.YELLOW,
    };

}
